#!/bin/bash


echo "************** systemDemo.sh result*************">>~/result.txt
MEM=`cat /proc/meminfo | grep MemTotal | awk -F" " '{print $2}'`
SWAP=`cat /proc/meminfo | grep SwapTotal | awk -F" " '{print $2}'`
MEM=$(($MEM+$SWAP))
echo "MEM="$MEM
echo "Total Memory is "$MEM >> ~/result.txt

MEM_KB=`top -n 1|  grep  'KiB Mem' | awk -F" " '{print $8,$4}'` 
SWAP_KB=`top -n 1|  grep  'KiB Swap' | awk -F" " '{print $7,$3}'`
echo "MEM_KB=="$MEM_KB >> ~/result.txt
echo "SWAP_KB=="$SWAP_KB >> ~/result.txt

process1=("vins/viwo_node" "pose_graph_optimization_node"  "evaluation_node" "fusion_result" "dr_odo" "seg_ros" "det_ros" "objectcluster" "segfusion_node" "slope" "bboxmerge" "zvision_lidar" "serial_date" "camera_pub" "realsense" "systemd" "syslogd" "nvmap" "rosout" "Xorg" "rosmaster" "tf")
process_length=${#process1[*]}
MEMSUM=0
MEMVSZ=0
MEMRSS=0
for ((i=0;i<${#process1[*]};i++))
do
	echo ${process1[i]}
	percent=`ps aux | grep ${process1[i]} | grep -v grep | awk -F" " '{print $4}'`
	VSZ=`ps aux | grep ${process1[i]} | grep -v grep | awk -F" " '{print $5}'`
	RSS=`ps aux | grep ${process1[i]} | grep -v grep | awk -F" " '{print $6}'`
	echo ${percent}
	totalPer=`echo $percent | sed 's/ /+/g' | bc`
	totalVSZ=`echo $VSZ | sed 's/ /+/g' | bc`  
        #sed 's/原字符串/替换字符串/g' bc 通过管道符
	totalRSS=`echo $RSS | sed 's/ /+/g' | bc`
	echo ""${totalPer}
	echo  "VSZ "$totalVSZ
	echo "RSS "$totalRSS
	result=`echo ${MEM} \* ${totalPer} / 100 | bc`
	MEMSUM=$(($result+$MEMSUM))
	MEMVSZ=$(($totalVSZ+$MEMVSZ))
	MEMRSS=$(($totalRSS+$MEMRSS))
	echo $MEMSUM"  VSZ="$MEMVSZ" RSS=="$MEMRSS
	echo "###Process "${process1[i]}" MEM usage percent="$percent"%  USAGE="$result" VSZ="$totalVSZ" KB RSS="$RSS" KB" >> ~/result.txt
done

for ((i=0;i<${#process1[*]};i++))
do
	echo ${process1[i]}
	percent=`ps aux | grep ${process1[i]} | grep -v grep | awk -F" " '{print $3}'`
	testVar=`echo $percent | sed 's/ /+/g' | bc`
	echo "@@@Process "${process1[i]}" CPU usage="$testVar >> ~/result.txt
done
